var express = require('express');
var db = require("../db/db");
var request = require("request");
var router = express.Router();
var yingshi = require("../db/yingshi")

/* GET home page. */
router.get('/', function(req, res, next) {
    res.send("<h1>哈哈</h1>")
});
// 查询接口
router.get("/chaxun",(req,res)=>{
  let data = {
    msg:"查询成功",
    data:{
      name:"张三",
      age:10
    }
  }
  res.json(data);
})
//专业查询
router.get("/search",(req,res)=>{
  let sql = "select * from web50";
  db(sql,(err,result)=>{
    if(err){
      throw err
    }
    console.log(666);
    res.json(result)
  })
})
// 电话查询
router.get("/phone",(req,res)=>{
  let sql = "select * from web50 where phone = '18220550305'";
  db(sql,(err,result)=>{
    if(err){
      throw err
    }
    console.log(666);
    res.json(result)
  })
})

//专业插入
router.get('/register',(req,res)=>{
    
    // let obj = req.query;
    // res.json(obj)
    let sql = `INSERT INTO web50 VALUES (null, '${obj.name}', '${obj.phone}')`;
    // let sql = `INSERT INTO web50 VALUES (null, '1111', '1111'),(null, '2222', '2222')`;

    db(sql,(err,result)=>{
      if(err){
        throw err
      }
      let data ={
        msg:"插入成功",
        code:1,
        data:result
      }
      res.json(data)
    })
})
//插入新闻接口
router.get("/inset",(req,res)=>{
  // request("rul",(err,response,body)=>{
  //       console.log('error:', err); // 返回错误信息
  //       console.log('statusCode:', response && response.statusCode); // 返回请求的状态码
  //       console.log('body:', body); // 返回回来的数据
  //       res.send(body);
  // })
  // res.json(yingshi)
  var str = "";
  yingshi.forEach(item => {
      str += `(null,"${item.title}"),`
  });
  str = str.slice(0,str.length-1);
  console.log(str);
  let sql = `INSERT INTO news VALUES ${str} `;
  db(sql,(err,result)=>{
      if(err){
        throw err;
      }
      res.json(result);
  })
})

module.exports = router;
